<?php

require './include/global.php';
require './include/validate.php';
require './include/all_left.php';

require_once './include/advisory_db.php';

$status = $_SESSION['status'];
$isTr = 'Trial' != $status && 'Standard' != $status;
$smarty->compile_check = true;

//Not A Recuiter
if($_SESSION[usertype] != "recruiter" ){
   echo "<meta http-equiv=\"refresh\" content=\"0;URL=errorall.php?errid=rc_1011\">";
   exit;	
}


$smarty->display('header.tpl');

//getvalues
if($_GET['country'] == ''){
	$_GET['country'] = 211;
}
$smarty->assign('s',$_GET);
//jobupdate
$smarty->assign('jobupdate_names',$UPDATE);
$smarty->assign('state',$state);
$smarty->assign('country',$country);
$smarty->assign('industry',$jobcategory);
$smarty->assign('jobfunction',$jobfunction);
$rec_mycontacts = array('-- SELECT ALL --','First Name','Last Name','Email','Specialty','Industry','Add Date');
$smarty->assign('rec_mycontacts',$rec_mycontacts);

$smarty->assign('isTr',$isTr);

//judge if current recruiter member has the group options
$groups = $DB->fetch_all("SELECT * FROM pa_contactgroup WHERE `cgp_rcid` = '".$_SESSION[uid]."'");
$groupOptions[] = '-- SELECT ALL --';
if(!empty($groups)){
	foreach($groups as $group){
		$groupOptions[$group['cgp_id']] = $group['cgp_title'];
	}
}
$smarty->assign('groups',$groupOptions);


$order = " ORDER BY myc_created  DESC";
$and_sql = "";

$addwhere = " AND myc_rcid  = '".$_SESSION[uid]."' ";
$where = "  WHERE 1=1 ".$addwhere.$and_sql;

if($_GET[action]=="search"){ 
	$arr = array();
	if($_GET[name]!=''){ $arr[] = " (`myc_firstname` like '%".mysql_real_escape_string($_GET[name])."%' OR  `myc_lastname` like '%".mysql_real_escape_string($_GET[name])."%') "; }
	if($_GET[groups]!='' && $_GET[groups]!='0') { $arr[] = " FIND_IN_SET('".mysql_real_escape_string($_GET[groups])."',`myc_gid`)  "; }
	if($_GET[city]!='') { $arr[] = " `myc_city` like  '%".mysql_real_escape_string($_GET[city])."%'  ";}
	if($_GET[state]!='0'&&$_GET[state]!='') { $arr[] = " `myc_state` = '".$_GET[state]."'  ";}
	if($_GET[country]!='0'&&$_GET[country]!='') { $arr[] = " `myc_country` = '".$_GET[country]."'  "; }
	if($_GET[industry]!='0'&&$_GET[industry]!='') { $arr[] = " `myc_industry` = '".$_GET[industry]."'  ";}
	if($_GET[speciality]!='0'&&$_GET[speciality]!='') { $arr[] = " (`myc_speciality1` = '".$_GET[speciality]."' OR `myc_speciality2` = '".$_GET[speciality]."' OR `myc_speciality3` = '".$_GET[speciality]."' ) ";}

	if($_GET[orderby]=='0'||empty($_GET[orderby])){ $by = "myc_id";}
	else{
		$ord   = array('','myc_firstname','myc_lastname','myc_email','myc_speciality1','myc_industry','myc_created');
		$by = $ord[$_GET[orderby]];
		$get[] = "orderby=$_GET[orderby]";
	}


	//print_r($arr);
	if(!empty($arr)){
		$implode  = @implode("AND", $arr);
		$implode  = " AND ".$implode;
		$where    .= $implode;
	}


    $order = " ORDER BY $by DESC";
}

if($_GET[action]=='delContact'){
	$err = '';
	  if(empty($_POST[delId])){
		  $err .= "Please select a contact<br>";
	  }
	
	if($err == ''){
		 $delIds = @implode(",",$_POST[delId]);
		 $sql = "DELETE FROM pa_mycontacts WHERE `myc_rcid` = '".$_SESSION['uid']."' AND `myc_id`IN (".$delIds.")";
		 $DB->query($sql);
	}
}

//echo $order;
$nav = new buildNav;
$total = $DB->fetch_one_array("SELECT COUNT(*) AS count FROM pa_mycontacts $where");
$nav->limit = 25;

$nav->total_result = $total[count];
$nav->execute("SELECT * FROM pa_mycontacts $where $order");
//echo "SELECT * FROM pa_job $where $order";
$page = $nav->pagenav();
//echo $sql2;
$i=0;
$datajob = array();
while($list2 = $DB->fetch_array($nav->sql_result)){
   $datajob[$i]              = $list2;
   $datajob[$i]['arefriends'] = (areFriends($list2['myc_rcid'],$list2['myc_fid']))?1:0;
   $datajob[$i][name]		 = $list2[myc_firstname]." ".$list2[myc_lastname];
   $datajob[$i][speciality]		 = $list2[myc_speciality1] ? $jobfunction[$list2[myc_speciality1]] : '&nbsp;';
   $datajob[$i][industry]		 = $list2[myc_industry] ? $jobcategory[$list2[myc_industry]] : '&nbsp;';
   $tmpGroups = @explode(",",$list2[myc_gid]);
   if(!empty($tmpGroups)){	
	    $_Names = array();
		foreach($tmpGroups as $gid){
			$_Names[] = $groupOptions[$gid];
		}
		$datajob[$i][group] = @implode(",",$_Names);
   }
   $i++;
}

$smarty->assign("contactlist",$datajob);
$smarty->assign("page",$page);
if($err!=''){
$smarty->assign("err",$err);
}
$smarty->assign("search",$reg_data);
$smarty->display('allleft.tpl');
$smarty->display('mycontacts.tpl');


//$smarty->display('allright.tpl');
$smarty->display('footer.tpl');
?>